package com.xcx.robot.mirai.factory.facade;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.mirai.entity.JX3API;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.jxapi.Result;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.KeyUtils;
import com.xcx.robot.util.ImageUtils;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.Image;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: 邢晨旭
 * @Date: 2021/6/22 09:33
 * @Description:
 */
@Service
public class SerendipityFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.SERENDIPITY;


    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        String name = KeyUtils.getKeyByWord(content,2);
        if(StringUtils.isEmpty(name)){
            return;
        }
        String s = HttpUtil.get(String.format(JX3API.SERENDIPITY,name));
        Result result = JSONObject.parseObject(s, Result.class);
        if (result.getCode() == 200) {
            Image image = receiver.uploadImage(ImageUtils.initImage(result.getData().getImage()));
            receiver.sendMessage(image);
        }else{
            receiver.sendMessage("没有查到哦,请确认奇遇名称是否正确");
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
